package com.qigao.mall.web.controller.app.merchant;

import com.qigao.mall.api.MerchantEnterService;
import com.qigao.mall.commons.dto.MerEnterApplyDto;
import com.qigao.mall.commons.dto.MerInfoDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.config.QiGaoProperties;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.MerEnterCreateVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/merchant/enter/")
@Api(tags = "App商户进件")
public class MerchantEnterController {

    private static final Logger logger = LoggerFactory.getLogger(MerchantEnterController.class);
    @Resource
    private MerchantEnterService merchantEnterService;
    @Resource
    private QiGaoProperties qiGaoProperties;

    //1.进件申请，返回协议签署链接。
    @PostMapping("/apply")
    public Result apply(@RequestBody MerEnterCreateVo merEnterCreateVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(merEnterCreateVo);
        try{
            MerEnterApplyDto merEnterApplyDto =new MerEnterApplyDto();
            BeanUtils.copyProperties(merEnterCreateVo, merEnterApplyDto);
            merEnterApplyDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            merEnterApplyDto.setPMerNo(qiGaoProperties.getMerNo());
            Result merResult=merchantEnterService.apply(merEnterApplyDto);
            return merResult;
        }catch (Exception e){
            logger.error("进件申请,返回协议签署链接发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //2.进件查询，先查本地数据库，处理中再查微信端。返回进件状态。
    @GetMapping("/findByMerNo/{merNo}")
    public Result findByMerNo(@PathVariable("merNo")String merNo){
        try{
            MerInfoDto merInfoDto=merchantEnterService.findByMerNo(merNo);
            if(merInfoDto==null ){
                return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
            }
            return Result.newSuccess(merInfoDto);
        }catch (Exception e){
            logger.error("按照商户号进件查询发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
